<?php
namespace zyj\weather;


/**
 * 和风天气免费
 * User: BF
 * Date: 2018/12/27
 * Time: 9:00
 */
class HfWeather extends Weather
{
    /**
     *    请求地址
     *
     * @var string
     */
    public $url = 'https://free-api.heweather.net/s6/weather/now?';

    public $key;

    public $username;

    public $t;


    /**
     * 实况天气
     * 根据KEY值请求
     * @var string $location 经纬度/中文城市名（如果是中文，则需要urlencode()）/城市代码、IP
     * return
     */
    public function requestByKey($location)
    {
        $key = $this->key;
        $curlPost = "key=".$key."&location=" . $location;
        $res = $this->HfCurl($curlPost);
        $result = json_decode($res, true);
        return $result['HeWeather6'][0];
    }

    /**
     * 实况天气
     * 根据Sign签名形式请求
     * @var string $location 经纬度/中文城市名（如果是中文，则需要urlencode()）/城市代码、IP
     * return
     */
    public function requestBySign($location)
    {
        $key = $this->key;
        $params['location'] = $location;
        $params['username'] = $this->username;
        $params['t'] = time();
        $sign = $this->getSignature($params, $key);
        $curlPost = "sign=".$sign."&location=".urlencode($params['location'])."&username=".$params['username']."&t=".$params['t'];
        $res = $this->HfCurl($curlPost);
        $result = json_decode($res, true);
        return $result['HeWeather6'][0];
    }

    /**
     * 和风天气签名生成算法-PHP版本
     * @param array $params API调用的请求参数集合的关联数组（全部需要传递的参数组成的数组），不包含sign参数
     * @param $secret 用户的认证 key
     * @return string 返回参数签名值
     */
    protected function getSignature($params, $secret) {
        $str = '';  //待签名字符串
        //先将参数以其参数名的字典序升序进行排序
        array_filter($params);
        unset($params['sign']);
        unset($params['key']);
        ksort($params);
        //遍历排序后的参数数组中的每一个key/value对
        foreach($params as $k => $v){
            $str .= $k . '=' . $v . '&';
        }
        $str = substr($str,0,strlen($str)-1);
        //将签名密钥拼接到签名字符串最后面
        $str .= $secret;
        //通过md5算法为签名字符串生成一个md5签名，该签名就是我们要追加的sign参数值
        return base64_encode(md5($str, true));
    }


    protected function HfCurl($curlPost)
    {

        //初始化请求链接
        $req=curl_init();
        //设置请求链接
        curl_setopt($req, CURLOPT_URL,$this->url.$curlPost);
        //设置超时时长(秒)
        curl_setopt($req, CURLOPT_TIMEOUT,3);
        //设置链接时长
        curl_setopt($req, CURLOPT_CONNECTTIMEOUT,10);
                curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($req, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($req,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($req, CURLOPT_HEADER, 0);

        $data = curl_exec($req);
//        //设置头信息
//        $headers=array( "Accept: application/json", "Content-Type: application/json;charset=utf-8" );
//        curl_setopt($req, CURLOPT_HTTPHEADER, $headers);
//
//        curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($req, CURLOPT_SSL_VERIFYHOST, false);
//        $data = curl_exec($req);
        curl_close($req);
        return $data;
    }
}